package com.snow.useviewdemo.function2.exposure;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;

import androidx.annotation.NonNull;

import com.snow.useviewdemo.R;


/**
 * Created by zhaoyingtao
 * Date: 2020/9/3
 * Describe: 曝光的view
 */
public class ExposureSubstitutionView extends View {
    private Context mContext;

    public ExposureSubstitutionView(@NonNull Context context) {
        super(context);
        this.mContext = context;
        Log.e("ExposureSubstitution", "=曝光的view==adInfo=null");
        setBackgroundColor(context.getResources().getColor(R.color.tt_red));
        getViewTreeObserver().addOnScrollChangedListener(
                new ViewTreeObserver.OnScrollChangedListener() {
                    @Override
                    public void onScrollChanged() {

                        Log.e("ExposureSubstitution", "=onScrollChanged=====================" + isVisibleOnScreen((Activity) mContext, ExposureSubstitutionView.this));
                    }
//                        // 可以先判断ScrollView中的mView是不是在屏幕中可见
//                        Rect scrollBounds = new Rect();
//                        mScrollView.getHitRect(scrollBounds);
//                        if (!mView.getLocalVisibleRect(scrollBounds)) {
//                            return;
//                        }
//
//                        // 再用封装好的工具类检查可见性是否大于50%
//                        if (VisibilityCheckUtil.check(mView)) {
//                            // do something
//                        }
//                    }
                });

    }

    /**
     * 判断view是否在应用可见位置
     * @param activity
     * @param view
     * @return
     */
    public static boolean isVisibleOnScreen(Activity activity, View view) {
        if (activity == null || view == null) {
            return false;
        }
        Rect screenRect = new Rect();
        activity.getWindow().getDecorView()
                .getGlobalVisibleRect(screenRect);
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        boolean inHorizontalBounds = screenRect.left <= location[0]
                && screenRect.right >= location[0] + view.getWidth();
        boolean inVerticalBounds = screenRect.top <= location[1]
                && screenRect.bottom >= location[1] + view.getHeight();
        return view.isShown() && inHorizontalBounds && inVerticalBounds;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        setMeasuredDimension(widthMeasureSpec, DisplayUtil.dip2px(mContext, 10));
    }

    //
    @Override
    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);//只会显示0，即使移除屏幕也不会输出8
        Log.e("ExposureSubstitution", "===onVisibilityChanged===oooo=" + visibility);
    }

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
//        Log.e("ExposureSubstitution", "===onWindowVisibilityChanged===1111=" + visibility);
        if (visibility == 0) {
            Log.e("ExposureSubstitution", "=此自运营View=曝光=");
        } else {
            Log.e("ExposureSubstitution", "=此自运营View=隐藏=");
        }
    }
}
